using UnityEditor;
using UnityEngine;
using System.Collections;


// Only compile if not using Unity iPhone
#if !UNITY_IPHONE || (UNITY_3_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_3_6 || UNITY_3_7 || UNITY_3_8 || UNITY_3_9)

public class lwEZUICtlInspector : UICtlInspector
{

	// Texture's rectangle displayed in the inspector
	private Rect rectTex = new Rect();
	// Texture's path displayed in the inspector
	private string statePath = "";
	// Reference to the texture displayed in the inspector (in order to not repaint each frame)
	private Texture2D constantTex = null;
	// True if we select the texture
	private bool isSelected = false;

	protected override void ShowSpriteSettings()
	{
		
		IPackableControl cont = (IPackableControl)control;	
		
		// Get the info for this state/element:
		stateInfo = cont.GetStateElementInfo(curState);

		// Put up a texture drop box:
		// Load the texture:
		if (stateInfo.stateObj.framePaths.Length < 1)
		{
			stateInfo.tex = null;
			constantTex = null;
		}
		else
		{
			if(!string.IsNullOrEmpty(stateInfo.stateObj.framePaths[0])
				&& System.IO.File.Exists(System.IO.Path.GetFullPath(GetImagesFolder() + stateInfo.stateObj.framePaths[0])))
			{
				if (statePath != stateInfo.stateObj.framePaths[0]) 
				{

					WWW image = new WWW("file://" + System.IO.Path.GetFullPath(GetImagesFolder() + stateInfo.stateObj.framePaths[0]));
					while(!image.isDone);
					DestroyImmediate(stateInfo.tex);
					Resources.UnloadUnusedAssets();
					stateInfo.tex = image.texture;
					stateInfo.tex.name = System.IO.Path.GetFullPath(GetImagesFolder() + stateInfo.stateObj.framePaths[0]);
					statePath = stateInfo.stateObj.framePaths[0];
					constantTex = stateInfo.tex;
				}
				else
				{
					stateInfo.tex = constantTex;
				}
			}
			else
				statePath = "";
		}


		BeginMonitorChanges();
		// Select the texture for the state:
		
		if(isSelected) {
			GUI.backgroundColor = new Color(0, 1, 0, 0.5f);
		}
		
		GUILayoutOption[] options = {GUILayout.ExpandWidth(true), GUILayout.MaxWidth(300)};
		
		if(stateInfo.tex == null)
			EditorGUILayout.LabelField("None texture is selected", GUI.skin.box, options);
		else
			EditorGUILayout.LabelField(new GUIContent(stateInfo.tex), GUI.skin.box, options);
		
		GUI.backgroundColor = new Color(1, 1, 1, 1.0f);
		
		if(rectTex != GUILayoutUtility.GetLastRect())
			rectTex = GUILayoutUtility.GetLastRect();
		
		if (Event.current.type == EventType.MouseDown && rectTex.Contains(Event.current.mousePosition) && stateInfo.tex != null) {
			isSelected = !isSelected;
			Repaint();
		}

		if(GUIUtility.keyboardControl == 0 && Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Delete && isSelected)
		{
			isSelected = false;
			DestroyImmediate(stateInfo.tex);
			constantTex = null;
			stateInfo.stateObj.framePaths[0] = "";
			statePath = "";
			Repaint();
		}
		
		EventType eventType = Event.current.type;
		
		if (eventType == EventType.DragUpdated || eventType == EventType.DragPerform)
		{
			if (rectTex.Contains(Event.current.mousePosition))
			{
				// Show a copy icon on the drag
				DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
				
				if (eventType == EventType.DragPerform)
				{
					DragAndDrop.AcceptDrag();
					//if(DragAndDrop.paths[0].StartsWith(lwBuildSettings.GetSpriteRootPath()))
					if (System.IO.Path.GetFullPath(DragAndDrop.paths[0]).Replace("\\", "/").StartsWith(System.IO.Path.GetFullPath(Application.dataPath + lwBuildSettings.GetSpriteRootPath()).Replace("\\", "/")))
					{
						WWW image = new WWW("file://" + System.IO.Path.GetFullPath(DragAndDrop.paths[0]));
						while(!image.isDone);
						DestroyImmediate(stateInfo.tex);
						Resources.UnloadUnusedAssets();
						stateInfo.tex = image.texture;
						//statePath = System.IO.Path.GetFullPath(DragAndDrop.paths[0]);
						statePath = System.IO.Path.GetFullPath(DragAndDrop.paths[0]).Replace("\\", "/").Substring(System.IO.Path.GetFullPath(GetImagesFolder()).Replace("\\", "/").Length);
						constantTex = stateInfo.tex;
					}
					else
						Debug.LogWarning("You didn't select an image in the source image folder : " + lwBuildSettings.GetSpriteRootPath());
				}

				Event.current.Use();
			}
		}
		
		EndMonitorChanges();
		
		// Re-assign the state info to the control:
		// If we have an available frame, assign the new GUID
		if (stateInfo.stateObj.framePaths.Length > 0)
		{
			stateInfo.stateObj.framePaths[0] = statePath;
		} // Else only create a new GUID element if the selection is non-null:
		else if (stateInfo.tex != null)
		{
			stateInfo.stateObj.framePaths = new string[1];
			stateInfo.stateObj.framePaths[0] = statePath;
		} // Else, don't do anything. Period.

		transitions = stateInfo.transitions;
	}

	static private bool isSettingLoaded = false;
	public string GetImagesFolder()
	{
		if (!isSettingLoaded)
		{
			lwBuildSettings.Load();
			isSettingLoaded = true;
		}
		return Application.dataPath + lwBuildSettings.GetSpriteRootPath();
	}
}

#else

// Keep Unity iPhone happy:
public class UICtlInspector : Editor
{
}


#endif	// END iPhone exclusion
